package com.tj.test.sys.controller;

import com.tj.test.sys.po.Question;
import com.tj.test.sys.service.IQuestionService;
import com.tj.test.sys.tool.ResponseMessage;
import com.tj.test.sys.tool.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@CrossOrigin
@RequestMapping("/Question")
@Api(tags = "Question")
public class QuestionController {
    @Autowired
    private IQuestionService service;

    @ApiOperation("查询所有题库信息")
    @RequestMapping(value = "/selectAllQuestion",method = RequestMethod.GET)
    @ResponseBody
    public ResponseMessage selectAllQuestion(){
        Map<String,List<Question>> map=new HashMap<>();
        List<Question> list=service.selectAllQuestion();
        if (!list.isEmpty()){
            //map.put("AllQuestion",list);
            System.out.println("aaaa");
            return Result.success("查询成功",list);
        }
        return Result.error("查询失败",false);
    }

    @ApiOperation("根据id查询题库信息")
    @RequestMapping(value = "/selectQuestionById",method = RequestMethod.GET)
    @ResponseBody
    public ResponseMessage selectQuestionById(int teacherid){
       // Map<String,List<Question>> map=new HashMap<>();
        List<Question> list=service.selectQuestionById(teacherid);
        if (!list.isEmpty()){
            //map.put("Question",list);
            return Result.success("查询成功",list);
        }
        return Result.error("查询失败",false);
    }

    @ApiOperation("插入题库信息")
    @RequestMapping(value = "/insertQuestion",method = RequestMethod.GET)
    @ResponseBody
    public ResponseMessage insertQuestion(Question question){
        int temp=0;
        if (question!=null){
            if (question.getTeacherid()!=0){
                temp=service.insertQuestion(question);
                if (temp>0) {return Result.success("插入题库信息成功",temp);}
                else  {return Result.error("该题库已存在",temp);}
            }
        }
        return Result.error("插入题库信息失败",temp);
    }

    @ApiOperation("删除题库信息")
    @RequestMapping(value = "/deleteQuestion",method = RequestMethod.GET)
    @ResponseBody
    public ResponseMessage deleteQuestion(Question question){
        int temp=0;
        if (question!=null){
            if (question.getQuestionid()!=0){
                temp=service.deleteQuestion(question);
                if (temp>0) {return Result.success("删除题库信息成功",temp);}
                else {return Result.error("无该题库",temp);}
            }
        }
        return Result.error("删除题库信息失败",temp);
    }

    @ApiOperation("修改题库信息")
    @RequestMapping(value = "/updateQuestion",method = RequestMethod.GET)
    @ResponseBody
    public ResponseMessage updateQuestion(Question question){
        int temp=0;
        if (question!=null){
            if (question.getQuestionid()!=0){
                temp=service.updateQuestion(question);
                if (temp>0) {return Result.success("更新题库信息成功",temp);}
                else {return Result.error("无该题库表信息",temp);}
            }
        }
        return Result.error("更新题库信息失败",temp);
    }
}
